package com.flyiu.data.consistency.synchronization;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;

import com.flyiu.data.consistency.service.DistributeService;

/**
 * 分布式事务同步器
 * 注册到一阶段事务中
 * 
 * @author flyiu
 *
 */
@Component
public class DistributedTransactionSynchronization extends TransactionSynchronizationAdapter {

	@Autowired
	private DistributeService service;
	
	public void afterCompletion(int status) {
		// 一阶段处理成功
		if (status == STATUS_COMMITTED) {
			service.preCommit();
			service.commit();
			// 一阶段处理失败
		} else if (status == STATUS_ROLLED_BACK) {
//			service.preRollback();
			service.rollback();
		}

	}
}
